package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.AppointmentStatusEnum;
import cn.wolfcode.util.ConsumptionStatusEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }


    //在预约页面点击 已到店并且有消费 就要预约单修改状态 和 获取该预约单数据存到 结算单的对象中
    //因为预约单的流水号 和 结算单的预约流水号是一致的所以我们通过这个条件来关联表 获取该预约对象的数据
    //如果没有预约流水号(唯一的) 就不知道获取的是哪一个预约单的数据
    @Override
    public Consumption save(String appointmentAno) {
        //创建当前时间
        Date date = new Date();
        //通过流水号获取数据
        Appointment appointment = appointmentService.selectByAno(appointmentAno);

        //因为我们有这个对象了就可以获取id 消费的状态为2
        appointmentService.updateStatus(appointment.getId() , Long.valueOf(AppointmentStatusEnum.CONSUME.getValue()));

        //创建一个结算单对象 来存储已有的数据
        Consumption consumption = new Consumption();
        //设置预约单的流水号
        consumption.setAppointmentAno(appointmentAno);
        //生成预约单的流水号 通过API 获取当前时间 加 随机数
        String format = DateUtil.format(date, "yyyyMMddHHmmss");
        String numbers = RandomUtil.randomNumbers(5);
        //设置消费单流水号
        consumption.setCno(format + numbers);
        //从预约单获取名字 存到消费单中
        consumption.setCustomerName(appointment.getContact_name());
        //从预约单获取电话 存到消费单中
        consumption.setCustomerTel(appointment.getContact_tel());
        //从预约单获取预约门店 存到消费单中
        consumption.setBusiness(appointment.getBusiness());
        //进店时间设置为当前时间
        consumption.setCheckinTime(date);
        //创建时间设置为当前时间
        consumption.setCreateTime(date);
        //创建人设置为当前登录用户(用户)
        consumption.setCreateUser(UserContext.getEmployee());
        consumptionMapper.insert(consumption);

        return consumption;
    }

    //结算时间 payTime  结算人的id payeeId  status设置为1待审核
    //审核时间 auditTime 审核人的id auditorId status设置为2归档 3坏账
    //如果是坏账就要扣除审核人的结算人的工资
    @Override
    public void updatePayeeMsg(Long id, Integer status, Date payTime, Long payeeId) {
        consumptionMapper.updatePayeeMsg(id,status,payTime,payeeId);
    }

    @Override
    public void updateAuditorMsg(Long id, Integer status, Date auditTime, Long auditorId) {
        if (status == ConsumptionStatusEnum.FAILURE.getValue()){
            //通过id 去获取该订单实际支付的金额 减去 员工的工资
        }
        consumptionMapper.updateAuditorMsg(id,status,auditTime,auditorId);
    }


}
